package com.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class MyChatClient {

	public static void main(String[] args) throws Exception {
		EventLoopGroup group = new NioEventLoopGroup();
		try{
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class).handler(new MyChatClientInitializer());

			//发起异步连接操作
			Channel ch = b.connect("localhost", 8899).sync().channel();

			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			for(;;) {
				ch.writeAndFlush(br.readLine() + "\r\n");
			}
		}finally{
			//优雅退出，释放nio线程组
			group.shutdownGracefully();
		}
	}

}
